package com.shundr.shipper.common.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseDialog;
import com.shundr.shipper.frame.constant.LContext;
import com.shundr.shipper.user.model.UserInfo;

/**
 * 
 * DialogAuth
 * 
 * @author larry 2015-4-16 下午7:04:38 
 * @version 1.0.0
 *
 */
public class DialogAuth extends BaseDialog {
	
	private Context mContext;
	private UserInfo mUserInfo;
	
	public DialogAuth(Context context,UserInfo userInfo){
		this.mContext = context;
		this.mUserInfo = userInfo;
	}

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		getDialog().requestWindowFeature((int) Window.FEATURE_NO_TITLE);
		if(mUserInfo == null)
			dismiss();
		View view = inflater.inflate(R.layout.dialog_go_auth, null);
		TextView tvTitle = (TextView)view.findViewById(R.id.tv_title);
		Button btnCancel = (Button)view.findViewById(R.id.btn_cancel);
		Button btnAuthPersonal = (Button)view.findViewById(R.id.btn_auth_personal);
		Button btnAuthCompany = (Button)view.findViewById(R.id.btn_auth_company);
		try {
			if(mUserInfo.getUserType() == 0){//司机
				btnAuthCompany.setVisibility(View.GONE);
				if(mUserInfo.getIdValidationStatus() == 0){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_auth_no));
					btnAuthPersonal.setVisibility(View.VISIBLE);
				}else if(mUserInfo.getIdValidationStatus() == 1){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_authing));
				}else if(mUserInfo.getIdValidationStatus() == 2){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_authed));
				}else if(mUserInfo.getIdValidationStatus() == 3){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_auth_rejected)
							+ "\n" + mUserInfo.getIdValidationMsg());
					btnAuthPersonal.setVisibility(View.VISIBLE);
				}
			}else{//货主
				if(mUserInfo.getCompanyValidationStatus() == 0){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_auth_no));
					btnAuthCompany.setVisibility(View.VISIBLE);
					btnAuthPersonal.setVisibility(View.VISIBLE);
				}else if(mUserInfo.getCompanyValidationStatus() == 1){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_authing));
				}else if(mUserInfo.getCompanyValidationStatus() == 2){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_authed));
				}else if(mUserInfo.getCompanyValidationStatus() == 3){//0为未认证，1为在审核，2为已审核，3为审核不通过
					tvTitle.setText(mContext.getResources().getString(R.string.tv_auth_status_auth_rejected)
							+ "\n" + mUserInfo.getCompanyValidationMsg());
					btnAuthCompany.setVisibility(View.VISIBLE);
					btnAuthPersonal.setVisibility(View.VISIBLE);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		btnAuthPersonal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				if(mUserInfo.getUserType() == 0){
//					startActivity(new Intent(mContext, AuthDriverActivity.class));//司机个人认证
				}else{
//					startActivity(new Intent(mContext, AuthOwnnerActivity.class));//货主个人认证
				}
			}
		});
		btnAuthCompany.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
//				startActivity(new Intent(mContext, AuthCompanyActivity.class));//货主企业认证
			}
		});
		
		Window dialogWindow = this.getDialog().getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);

        /*
         * lp.x与lp.y表示相对于原始位置的偏移.
         * 当参数值包含Gravity.LEFT时,对话框出现在左边,所以lp.x就表示相对左边的偏移,负值忽略.
         * 当参数值包含Gravity.RIGHT时,对话框出现在右边,所以lp.x就表示相对右边的偏移,负值忽略.
         * 当参数值包含Gravity.TOP时,对话框出现在上边,所以lp.y就表示相对上边的偏移,负值忽略.
         * 当参数值包含Gravity.BOTTOM时,对话框出现在下边,所以lp.y就表示相对下边的偏移,负值忽略.
         * 当参数值包含Gravity.CENTER_HORIZONTAL时
         * ,对话框水平居中,所以lp.x就表示在水平居中的位置移动lp.x像素,正值向右移动,负值向左移动.
         * 当参数值包含Gravity.CENTER_VERTICAL时
         * ,对话框垂直居中,所以lp.y就表示在垂直居中的位置移动lp.y像素,正值向右移动,负值向左移动.
         * gravity的默认值为Gravity.CENTER,即Gravity.CENTER_HORIZONTAL |
         * Gravity.CENTER_VERTICAL.
         * 
         * 本来setGravity的参数值为Gravity.LEFT | Gravity.TOP时对话框应出现在程序的左上角,但在
         * 我手机上测试时发现距左边与上边都有一小段距离,而且垂直坐标把程序标题栏也计算在内了,
         * Gravity.LEFT, Gravity.TOP, Gravity.BOTTOM与Gravity.RIGHT都是如此,据边界有一小段距离
         */
        lp.width = LContext.width; // 宽度
        lp.height = 300; // 高度
        dialogWindow.setAttributes(lp);
		return view;
	}
	
}
